$(function(){
	$('#tt').datagrid({
		url:'/douying/action/getListPage'
	});
});

function doAdd(){
	$('#addForm').form('clear');
	$('#addWin').window('open');
}

function doEdit(){
	var rows = $("#tt").datagrid("getSelections");
    if (rows&&rows.length>1){
    	$.messager.alert("提示", "请先选择一条记录");
		return;
    }
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	$('#idId').val(record.id);
	$('#douyinShortId').val(record.douyinShortId);
	$('#doNum').val(record.doNum);
	$('#videoKeyword').val(record.videoKeyword);
	$('#douyinUid').val(record.douyinUid);
	$('#awemeId').val(record.awemeId);
	$('#addWin').window('open');
}

function doDel(){
	var rows = $("#tt").datagrid("getSelections");
    if (rows&&rows.length>0){
    	var arrayId = [];
    	for(var i=0;i<rows.length;i++){
    		arrayId.push(rows[i].id);
    	}
    	var ids = arrayId.join(",");
	    showConfirm(market.content.title, market.content.confirm, function(){
		  $.post('/douying/action/delete',{ids:ids}, function(data) {
		  	if (data.return_code == '1') {
				top.showMsg(market.content.title, data.return_msg, alert);
				$("#tt").datagrid('reload');
				$('#tt').datagrid('clearSelections');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		  });
	   });
    } else {
    	showMsg(market.content.title, market.content.tips, alert);
    }
}

function doDetail(){
	var rows = $("#tt").datagrid("getSelections");
    if (rows&&rows.length>1){
    	$.messager.alert("提示", "请先选择一条记录");
		return;
    }
    
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	var id=record.id;
	$('#selectWin').window('open');
	$('#actionId').val(id);
	
	$('#stepTable').datagrid({
		url:'/douying/action/step/getListPage?actionId='+id,
	});
}

function doSearch(){
	var nshortId=$('#nshortId').val();
	var nkeyword=$('#nkeyword').val();
	$('#tt').datagrid({
		url:'/douying/action/getListPage',
		queryParams:{
			douyinShortId:nshortId,
			videoKeyword:nkeyword
		}
	});
	$('#tt').datagrid('unselectAll');
}

function save(){
	var id=$('#idId').val();
	var url='/douying/action/update';
	if(id==null||id==''){
		url='/douying/action/save';
	}
	
	$('#addForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addWin").window('close');
				$("#tt").datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
}


function cancel(){
	$('#addWin').window('close');
}

function formatStatus(value){
	if(value==1){
		return '启用';
	} else {
		return '关闭';
	}
}

function searchStep(){
	var stepName=$('#stepName').val();
	var actionId=$('#actionId').val();
	$('#stepTable').datagrid({
		url:'/douying/action/step/getListPage',
		queryParams:{
			stepName:stepName,
			actionId:actionId
		}
	});
	$('#stepTable').datagrid('unselectAll');
}

function toAddStep(){
	$('#addStepForm').form('clear');
	var actionId=$('#actionId').val();
	$('#stepActionId').val(actionId);
	$('#status').combobox('select',1);
	$('#stepNum').combobox('select','6');
	$('#addStepWin').window('open');
}

function saveStep(){
	var id=$('#stepId').val();
	var url='/douying/action/step/update';
	if(id==null||id==''){
		url='/douying/action/step/save';
	}
	
	$('#addStepForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addStepWin").window('close');
				$("#stepTable").datagrid('reload');
				$('#stepTable').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
}

function cancelStep(){
	$('#addStepWin').window('close');
}

function toEditStep(){
	var rows = $("#stepTable").datagrid("getSelections");
    if (rows&&rows.length>1){
    	$.messager.alert("提示", "请先选择一条记录");
		return;
    }
	var record = $('#stepTable').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	$('#stepId').val(record.id);
	$('#seq').val(record.seq);
	$('#stepNum').combobox('select',record.stepNum);
	$('#stepNum').val(record.stepNum);
	$('#delayTime').val(record.delayTime);
	$('#delayRand').val(record.delayRand);
	$('#countMin').val(record.countMin);
	$('#countMax').val(record.countMax);
	$('#hasNum').val(record.hasNum);
	$('#total').val(record.total);
	$('#status').combobox('select',record.status);
	$('#addStepWin').window('open');
}

function deleteStep(){
	var rows = $("#stepTable").datagrid("getSelections");
    if (rows&&rows.length>0){
    	var arrayId = [];
    	for(var i=0;i<rows.length;i++){
    		arrayId.push(rows[i].id);
    	}
    	var ids = arrayId.join(",");
	    showConfirm(market.content.title, market.content.confirm, function(){
		  $.post('/douying/action/step/delete',{ids:ids}, function(data) {
		  	if (data.return_code == '1') {
				top.showMsg(market.content.title, data.return_msg, alert);
				$("#stepTable").datagrid('reload');
				$('#stepTable').datagrid('clearSelections');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		  });
	   });
    } else {
    	showMsg(market.content.title, market.content.tips, alert);
    }
}